/*---- SPECIES -----*/
function Species(sName, vName, stateList){

	this.scientificName = sName;
	this.vernacularName = vName;
	this.stateList = stateList;
 	this.itisLink = 'http://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=all&search_kingdom=every&search_span=exactly_for&categories=All&source=html&search_credRating=All&search_value=';
	this.nsLink = 'http://www.natureserve.org/explorer/servlet/NatureServe?searchName=';
 }

Species.prototype.GetSpeciesLinkBoxHTML = function(){

	var html = '';
	
	var n = this.stateList.length;
 
	for(var i=0; i<n; i++){
	
		var state = this.stateList[i];
		
		state = state.replace(/\s+$|^\s+/,'');
		state = state.replace(/\s/,'_');
		html += '<a href="http://www.wildlifeactionplans.org/' + state.toLowerCase() + '.html" target="_blank">' + state + '</a>';
		if(i < n-1) {
			html += ', ';
		}
		else {
			html += '<br/><br/>' + "\n";
		}
	}
	
	 
 
	

	
	html += '<div style="margin-top:10px;padding-top:10px;border-top:1px solid #007756;display:none;">';
	html += 'Other resources:<br/><br/>';
 
	html += '&raquo;<a href="'  + this.nsLink + this.scientificName + '" target="_blank">NatureServe Report</a><br/><br/>';
	html += '&raquo;<a href="' + this.itisLink + this.scientificName + '" target="_blank">ITIS Report</a><br/><br/>';
	html += '</div>';
	return html;
	
}

/*---- SPECIES LIST -----*/
function SWAPSpeciesList(speciesInfoPanelID, sciNameSpanID, vNameSpanID, mapID, nsMapID, linkBoxID){

	this.speciesInfoPanelID = speciesInfoPanelID;
	this.sciNameSpanID = sciNameSpanID;
	this.vNameSpanID = vNameSpanID;
	this.speciesGeoMapID = mapID;
	this.speciesLinkBoxID = linkBoxID;
	this.nsMapID = nsMapID;
	this.nsURL = '/spinfo?source=natureServe&sname=';
	this.listOptions = {};
	this.SetListOptions();
}

SWAPSpeciesList.prototype.SetListOptions = function(){
	this.listOptions['sortColumn'] = 0;
	this.listOptions['width'] = 350;
	this.listOptions['page'] = 'enable';
    this.listOptions['pageSize'] = 13;
    this.listOptions['pagingSymbols'] = {prev: 'prev', next: 'next'};
    this.listOptions['pagingButtonsConfiguration'] = 'auto';
	this.listOptions['allowHtml'] = true;
	this.listOptions['showRowNumber'] = true;
}

SWAPSpeciesList.prototype.PrintSpeciesList = function (list, totalSpecies, speciesListID){
 
	var data = new google.visualization.DataTable(); 
 
	if(totalSpecies > 0){ 	
		var i = 0;
		data.addColumn('string', 'Species Count: ' + totalSpecies);
		//data.addColumn('string', 'Common Name');
		data.addRows(totalSpecies);
		for(var sciName in list){
			data.setValue(i,0, sciName);
			//data.setValue(i,1, list[sciName]);
			i++;
		}	
	}

	
	var table = new google.visualization.Table(document.getElementById(speciesListID));
	google.visualization.events.addListener(table, 'select', function(e) {    
	   var xy = table.getSelection();
	   if(xy.length > 0){
			var item = xy[0];
			var row = item.row;
			var column = 0;
			var dataItem = data.getValue(row, column);  
			speciesname_onclick(dataItem);	
		}
     
    });
	table.draw(data, this.listOptions);
	
}
	
SWAPSpeciesList.prototype.ShowSpeciesInformation = function(species){


	
	var htmlOutput = ''; 
 
	$('#' + this.sciNameSpanID).html(species.scientificName);
	$('#' + this.vNameSpanID).html(species.vernacularName);
	$('#' + this.speciesInfoPanelID).show();
 
	var html = species.GetSpeciesLinkBoxHTML();

	$('#' + this.speciesLinkBoxID).html(html); 
	this.DrawSpeciesGeoMap(species.scientificName, species.stateList);
	this.DrawSpeciesNSMap(species.scientificName);
	
}

	
SWAPSpeciesList.prototype.ShowLimitedSpeciesInformation = function(species){

	var htmlOutput = ''; 
 
	$('#' + this.sciNameSpanID).html(species.scientificName);
	$('#' + this.vNameSpanID).html(species.vernacularName);
	$('#' + this.speciesInfoPanelID).show();
 
	var html = species.GetSpeciesLinkBoxHTML();

	//$('#' + this.speciesLinkBoxID).html(html); 
	this.DrawSpeciesGeoMap(species.scientificName, species.stateList);	 
	
}

SWAPSpeciesList.prototype.CloseSpeciesInformationPanel = function(){

	$('#' + this.speciesGeoMapID).empty();
	$('#' + this.nsMapID).empty();
	$('#' + this.speciesInfoPanelID).hide();
}	
	
SWAPSpeciesList.prototype.DrawSpeciesGeoMap = function(scientificName, stateList){
 
 
	var n = stateList.length;
   $('#' + this.speciesGeoMapID).html('<img src="/style/ajax-loader-long2.gif" alt="loading..." />');
	var data = new google.visualization.DataTable();
    
	data.addColumn('string', 'State');
	data.addColumn('number', 'Noted as GCN');
	data.addRows(n);
	for(var i=0; i<n; i++){
		var state = stateList[i];
		state = state.replace(/^\s/,'');
		data.setValue(i,0,state);
		data.setValue(i,1,1);
	}
 
    var options = {};     
    options['region'] = 'US';
    options['width'] = 375;
    options['height'] = 256;
    options['colors'] = [0xAA5050, 0xAA8050];
    options['dataMode'] = 'regions';
	options['showLegend'] = false;
    
    var geomap = new google.visualization.GeoMap(document.getElementById(this.speciesGeoMapID));
    
	geomap.draw(data,options);
	
}

SWAPSpeciesList.prototype.DrawSpeciesNSMap = function(scientificName){
	
	$('#' + this.nsMapID).html('<img src="/style/ajax-loader-long2.gif" alt="loading..." />');
	var _url = this.nsURL + escape(scientificName);

	$.ajax({		
		url: _url,
		timeout: 10*1000,
		success: function(html) {
			//data = $.parseJSON(string);
		 	html = '<div style="background:#ffffff;padding:5px;border:1px solid #668877;">' + html + '</div>';
		    $('#' + splist.nsMapID).html(html);
		},
		error: function(msg){			 
			 $('#' + splist.nsMapID).html('<div style="width:450px;">There was an error processing the NatureServe services</div>');
		}
	});
	
}

SWAPSpeciesList.prototype.ShowSpeciesSearchResult = function(searchTerm){

	if(searchTerm == '') {
		return false;
	}

	var htmlOutput = ''; 
	 
	$('#speciesSearchTerm').html(searchTerm);	 
    var mainDiv = $('#speciesSearchResultPanel');
	
	var data = new google.visualization.DataTable();
	data.addColumn('string', 'Scientific Name');
	//data.addColumn('string', 'Common Name');
	
	mainDiv.show();
	var spList = new Array();
 	
	var n = 0;
	var spList = new Array();
	for(var state in pchartData){
	
		var list = pchartData[state];
		var totalSpecies = 0;
		
 
		for(var taxa in list){	
			 
			if(list[taxa].count > 0){
				var templist = list[taxa].list;
				for(var key in templist){	
		
					var sciName = templist[key].value[0];
					var commonName = templist[key].value[1];

					if( (sciName.toLowerCase().match(searchTerm.toLowerCase()) ) && spList[sciName] == undefined){
						data.addRow();
						spList[sciName] = commonName;						
						data.setValue(n,0,sciName);
						n++;
					} 
				}
			}		
		}
	}
	
	 
	
	$('#speciesSearchResultCount').html(n + ' species matched search term');
	
	var options = {};
	options['sortColumn'] = 0;
	options['width'] = 350;
	options['page'] = 'enable';
    options['pageSize'] = 15;
    options['pagingSymbols'] = {prev: 'prev', next: 'next'};
    options['pagingButtonsConfiguration'] = 'auto';
	options['allowHtml'] = true;
	options['showRowNumber'] = true;
	var table = new google.visualization.Table(document.getElementById('speciesSearchResultBox'));
	google.visualization.events.addListener(table, 'select', function(e) {    
	   var xy = table.getSelection();
	   if(xy.length > 0){
			var item = xy[0];
			var row = item.row;
			var column = 0;
			var dataItem = data.getValue(row, column); 
			species_search_onclick(dataItem);	
		}
     
    });
	
	table.draw(data, options);
	
	 
}

SWAPSpeciesList.prototype.CloseSpeciesSearchResultPanel = function() {
	$('#speciesSearcResultBox').empty();
	$('#speciesSearchResultPanel').hide();
}


SWAPSpeciesList.prototype.CloseMissingSpeciesPanel = function() {
	$('#missingSpeciesContent').empty();
	$('#missingSpeciesPanel').hide();
}

SWAPSpeciesList.prototype.ShowRecommendedSpeciesForCurrentState = function(){

	this.ShowRecommendedSpecies(ctl.currState);
}

SWAPSpeciesList.prototype.ShowRecommendedSpecies = function(state){


	var htmlOutput = ''; 

	 
    var mainDiv = $('#missingSpeciesPanel');
	
	var data = new google.visualization.DataTable();
	data.addColumn('string', 'Recommended State');
	data.addColumn('string', 'Species');
	data.addColumn('number', 'Score');
	data.addColumn('string', 'Adjacent States that Assigned SGCN Status');
	
	mainDiv.show();
	 
	var i = 0;
	var row = 0;
 
	if(state == 'All'){
	
		for(var state in missingSpecies){
		
			var list = missingSpecies[state];
		 
			for(var species in list){

				//for(var i=0; i<n; i++){	
				
					//var species = list.Species[i++];
				if(list[species].Count > 3){
						var stliststr = '';
						var sti = 0;
					 
						data.addRow();
						data.setValue(row, 0, state);
						data.setValue(row, 1, species);
						data.setValue(row, 2, list[species].Count);
						var stlist = list[species].AdjacentStates;
						var stn = stlist.length;
						for(var st in stlist){
							stliststr += stlist[st];							
							if(sti++ < stn-1) { stliststr += ', '; }
						}
						data.setValue(row, 3, stliststr);
						row++;
						
				}
				 
				 
			}
		 
			
		}
		
	}
	else {
		
		var list = missingSpecies[state];
		for(var species in list){
			if(list[species].Count > 3){
							var stliststr = '';
							var sti = 0;
						 
							data.addRow();
							data.setValue(row, 0, ctl.currState);
							data.setValue(row, 1, species);
							data.setValue(row, 2, list[species].Count);
							var stlist = list[species].AdjacentStates;
							var stn = stlist.length;
							for(var st in stlist){
								stliststr += stlist[st];
								
								if(sti++ < stn-1) { stliststr += ', '; }
							}
							data.setValue(row, 3, stliststr);
							row++;
							
			}
		}
		 
	}
	$('#missingSpeciesCount').html('<br/>Total count: ' + row);

	var options = {};
	options['sortColumn'] = 0;
	options['width'] = 640;
	options['page'] = 'enable';
    options['pageSize'] = 10;
    options['pagingSymbols'] = {prev: 'prev', next: 'next'};
    options['pagingButtonsConfiguration'] = 'auto';
	options['allowHtml'] = true;
	options['showRowNumber'] = true;
	var table = new google.visualization.Table(document.getElementById('missingSpeciesContent'));
	google.visualization.events.addListener(table, 'select', function(e) {    
	   var xy = table.getSelection();
	   if(xy.length > 0){
			var item = xy[0];
			var row = item.row;
			var column = 1;
			var sName = data.getValue(row, column); 
			var state = data.getValue(row, 0);
			missing_species_onclick(sName, state);	
		}
     
    });
	
	table.draw(data, options);
	
	 
}


